$(document).ready(function () {

    //TreeCustomBehavior.init();

    //  if ($('.tree-administration ').length > 0) {
    /*TreeCustomBehavior = {
     bindEvents: function() {*/

    /*var checked = $(". ui-icon-check").parent("div.ui-chkbox"),
        readCh  = checked.find("ui-chkbox");
    if (checked.length > 0) {
        readCh.find("span").addClass("ui-icon-check");
    }*/

    PrimeFaces.widget.VerticalTree.prototype.bindEvents = function () {
        var e = this,
            b = this.jqId + " .ui-tree-toggler",
            a = this.jqId + " .ui-tree-selectable .ui-treenode-label",
            c = this.jqId + " .ui-treenode-content";
        $(document).off("click", b).on("click", b, null, function (h) {
            var f = $(this),
                g = f.closest("li");
            if (f.hasClass(e.cfg.collapsedIcon)) {
                e.expandNode(g)
            } else {
                e.collapseNode(g)
            }
        });
        if (this.cfg.highlight && this.cfg.selectionMode) {
            $(document).off("mouseout.tree mouseover.tree", a).on("mouseout.tree", a, null, function () {
                var f = $(this);
                f.removeClass("ui-state-hover");
                if (e.isCheckboxSelection()) {
                    f.siblings("div.ui-chkbox").children("div.ui-chkbox-box").removeClass("ui-state-hover")
                }
            }).on("mouseover.tree", a, null, function () {
                var f = $(this);
                $(this).addClass("ui-state-hover");
                if (e.isCheckboxSelection()) {
                    f.siblings("div.ui-chkbox").children("div.ui-chkbox-box").addClass("ui-state-hover")
                }
            })
        }
        if (this.isCheckboxSelection()) {
            var d = this.jqId + " .ui-chkbox-box";
            $(document).off("mouseout.tree-checkbox mouseover.tree-checkbox click.tree-checkbox", d).on("mouseout.tree-checkbox", d, null, function () {
                $(this).removeClass("ui-state-hover").parent().siblings("span.ui-treenode-label").removeClass("ui-state-hover")
            }).on("mouseover.tree-checkbox", d, null, function () {
                $(this).addClass("ui-state-hover").parent().siblings("span.ui-treenode-label").addClass("ui-state-hover")
            })
        }
        $(document).off("click", "div.ui-chkbox").on("click", "div.ui-chkbox", function (f) {
            e.nodeClick(f, $(this).parent())
        });
        $(document).off("click", "div.ui-read-checkbox")
            .on("click", "div.ui-read-checkbox", null, function () {
                TreeCustomBehavior.readCBClicked($(this));
        });
    }


});

TreeCustomBehavior = {
    init: function () {
        var d = $('.tree-administration-btn');
        if (!d.hasClass('ui-state-active')) {
            return false;
        }

        var cc = '<div class="ui-read-checkbox ui-widget" style="margin-left: 3px;"><div class="ui-chkbox-box ui-widget ui-corner-all ui-state-default" title="Permite modificare"><span class="ui-chkbox-icon ui-c"></span></div></div>',
            trc = $('.tree-read-checkbox'),
            dc = trc.find('div.ui-chkbox');

        dc.after(cc);

        /*var r = $('.tree-administration').find("> ul > li > span"),
         //   rd = r.find("div.ui-chkbox-box"),
            rs = r.find("span.ui-chkbox-icon");

        //rd.addClass('tree-root-disabled');
        rs.addClass("ui-icon ui-icon-check");*/
        //this.initSelection();
    },

    initSelection: function () {
        this.selectionHolder = $("[id$=':checkbox_read-selection']");
        var a = this.selectionHolder.val();
        this.selections = a === "" ? [] : a.split(",");

        this.preselectCheckbox();
    },

    preselectCheckbox: function () {
        this.selections.forEach(function (o) {
            //var a = $('li.ui-treenode').attr("data-rowkey") === o;
            var d = $("[data-rowkey=" + o + "]");
            TreeCustomBehavior.check(d.find('> span'));
        });

    },

    readCBClicked: function (d) {
        var a = d.find("> .ui-chkbox-box > .ui-chkbox-icon").hasClass("ui-icon-check"),
            p = d.parent();
        if (a) {
            this.uncheck(p);
        } else {
            this.check(p);
        }
    },

    check: function (d) {
        var b = d.children(".ui-read-checkbox").children(".ui-chkbox-box"),
            a = b.children(".ui-chkbox-icon"),
            c = d.closest(".ui-treenode"),
            e = this.getRowKey(c);
        b.removeClass("ui-state-hover");
        a.addClass("ui-icon ui-icon-check");
        this.addToSelection(e);
        c.removeClass("ui-treenode-hasselected ui-treenode-unselected").addClass("ui-treenode-selected").attr("aria-checked", true).attr("aria-selected", true)
    },
    uncheck: function (d) {
        var b = d.children(".ui-read-checkbox").children(".ui-chkbox-box"),
            a = b.children(".ui-chkbox-icon"),
            c = d.closest(".ui-treenode"),
            e = this.getRowKey(c);
        b.removeClass("ui-state-hover");
        a.removeClass("ui-icon ui-icon-check");
        this.removeFromSelection(e);
        c.removeClass("ui-treenode-hasselected ui-treenode-selected").addClass("ui-treenode-unselected").attr("aria-checked", false).attr("aria-selected", false)
    },

    unSelectAllNodes: function() {
        this.selectionHolder = null;
        this.selections = null;
        var c = $(".ui-chkbox-box"),
            a = c.children(".ui-chkbox-icon");
        c.removeClass("ui-state-hover");
        a.removeClass("ui-icon ui-icon-check");
    },

    addToSelection: function (a) {
        if (!PrimeFaces.inArray(this.selections, a)) {
            this.selections.push(a)
        }
        this.writeSelections();
    },
    removeFromSelection: function (a) {
        this.selections = $.grep(this.selections, function (b) {
            return b !== a
        });
        this.writeSelections();
    },
    writeSelections: function () {
        this.selectionHolder.val(this.selections.join(","))
    },
    getRowKey: function (a) {
        return a.attr("data-rowkey")
    }
};

